package com.ruoyi.system.message.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.message.domain.MiniMessage;
import com.ruoyi.system.message.mapper.MiniMessageMapper;
import com.ruoyi.system.message.service.IMiniMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 消息提醒Service业务层处理
 *
 * @author lhj
 * @date 2024-02-18
 */
@Service
public class MiniMessageServiceImpl implements IMiniMessageService {
    @Autowired
    private MiniMessageMapper miniMessageMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询消息提醒
     *
     * @param id 消息提醒主键
     * @return 消息提醒
     */
    @Override
    public MiniMessage selectMiniMessageById(String id) {
        miniMessageMapper.updatereadOrNot(id);
        return miniMessageMapper.selectMiniMessageById(id);
    }

    /**
     * 查询消息提醒列表
     *
     * @param miniMessage 消息提醒
     * @return 消息提醒
     */
    @Override
    public List<MiniMessage> selectMiniMessageList(MiniMessage miniMessage) {
        if (miniMessage.getReminderTime() == null) {
            miniMessage.setReminderTime(DateUtil.date());
        }
//        miniMessage.setReadOrNot(0);
        List<SysUser> sysUsers = sysUserMapper.selectUserList(new SysUser());
        Map<Long, String> userMap = sysUsers.stream().collect(Collectors.toMap(user -> user.getUserId(), value -> value.getNickName()));
        miniMessage.setRecipient(SecurityUtils.getUserId());
        PageUtils.startPage();
        List<MiniMessage> list = miniMessageMapper.selectMiniMessageList(miniMessage);
        list.forEach(value -> {
            Long recipient = value.getRecipient();
            String nickName = userMap.get(recipient);
            value.setRecipientName(nickName);
        });
        return list;
    }

    /**
     * 新增消息提醒
     *
     * @param miniMessage 消息提醒
     * @return 结果
     */
    @Override
    public int insertMiniMessage(MiniMessage miniMessage) {
        miniMessage.setId(IdUtil.getSnowflakeNextIdStr());
        miniMessage.setCreateBy(SecurityUtils.getUsername());
        miniMessage.setCreateTime(DateUtils.getNowDate());
        return miniMessageMapper.insertMiniMessage(miniMessage);
    }

    /**
     * 修改消息提醒
     *
     * @param miniMessage 消息提醒
     * @return 结果
     */
    @Override
    public int updateMiniMessage(MiniMessage miniMessage) {
        miniMessage.setUpdateTime(DateUtils.getNowDate());
        return miniMessageMapper.updateMiniMessage(miniMessage);
    }

    /**
     * 批量删除消息提醒
     *
     * @param ids 需要删除的消息提醒主键
     * @return 结果
     */
    @Override
    public int deleteMiniMessageByIds(String[] ids) {
        return miniMessageMapper.deleteMiniMessageByIds(ids);
    }

    /**
     * 删除消息提醒信息
     *
     * @param id 消息提醒主键
     * @return 结果
     */
    @Override
    public int deleteMiniMessageById(String id) {
        return miniMessageMapper.deleteMiniMessageById(id);
    }

    @Override
    public void insertData(MiniMessage miniMessage) {
        miniMessage.setId(IdUtil.getSnowflakeNextIdStr());
        miniMessage.setCreateBy("admin");
        miniMessage.setCreateTime(DateUtils.getNowDate());
        miniMessageMapper.insertMiniMessage(miniMessage);
    }

    @Override
    public int oneClickRead(Long userId) {
        return miniMessageMapper.oneClickRead(userId);
    }

    @Override
    public Long unreadCount(Long userId) {
        return miniMessageMapper.selectCount(userId);
    }
}
